package com.ruoyi.contract.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ice.common.model.IceShowConf;
import com.ruoyi.base.domain.SProjectFee;
import com.ruoyi.base.service.ISProjectFeeService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.sign.RsaUtils;
import com.ruoyi.contract.domain.ConRule;
import com.ruoyi.contract.service.ConRuleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.net.URLEncoder;
import java.util.List;

@RestController
@RequestMapping("/contract/rules")
public class ConRuleController extends BaseController {

    @Autowired
    ConRuleService conRuleService;

    @Autowired
    ISProjectFeeService projectFeeService;

    @Value("${other.ice.requestUrl}")
    private String iceUrl;

    @Value("${ice.app}")
    private String iceApp;

    @Value("${other.ice.privateKey}")
    private String privateKey;

    @GetMapping("/list")
    public TableDataInfo list(ConRule conRule){
        startPage();
        QueryWrapper<ConRule> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .eq(StringUtils.isNotNull(conRule.getConId()),ConRule::getConId,conRule.getConId());
        List<ConRule> list = conRuleService.list(queryWrapper);

        return getDataTable(list);
    }


    @GetMapping("/getIceUrl/{id}")
    public AjaxResult getIceUrl(@PathVariable Long id) throws Exception {
        ConRule conRule = conRuleService.getById(id);
        if(StringUtils.isNotNull(conRule)&&StringUtils.isNotNull(conRule.getIceId())){


            return AjaxResult.success(conRule.getIceId());
        }
        return AjaxResult.error();
    }

    @GetMapping("/iceConfig/detail/{id}")
    public IceShowConf confDetail(@PathVariable Long id){

        return conRuleService.getIceConfig(id);

    }

    @GetMapping("/getOne/{id}")
    public AjaxResult getOne(@PathVariable Long id){
        return AjaxResult.success(conRuleService.getById(id));
    }

    @GetMapping("/labelProjectFeeByConId/{conId}")
    public AjaxResult labelProjectFeeByConId(@PathVariable Long conId){
        ConRule conRule = conRuleService.getById(conId);

        String projectType = conRule.getProjectType();

        SProjectFee projectFee = new SProjectFee();

        projectFee.setProjectType(projectType);

        return projectFeeService.label(projectFee);

    }

    @PostMapping("/add")
    @Log(title = "新增合同规则", businessType = BusinessType.INSERT)
    @PreAuthorize("@ss.hasPermi('contract:rules:add')")
    public AjaxResult add(@RequestBody ConRule conRule){
        try {
            return conRuleService.insert(conRule);
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
    }

    @PutMapping("/edit")
    @Log(title = "修改合同规则", businessType = BusinessType.UPDATE)
    @PreAuthorize("@ss.hasPermi('contract:rules:edit')")
    public AjaxResult edit(@RequestBody ConRule conRule){
        try {
            return conRuleService.updateRules(conRule);
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
    }

    @DeleteMapping("/remove/{ids}")
    @Log(title = "删除合同规则", businessType = BusinessType.DELETE)
    @PreAuthorize("@ss.hasPermi('contract:rules:remove')")
    public AjaxResult remove(@PathVariable Long[] ids){
        try {
            return conRuleService.removeIds(ids);
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
    }


    @GetMapping("/selectConRuleByProjectId/{projectId}")
    public AjaxResult selectConRuleByProjectId(@PathVariable Long projectId){
        return AjaxResult.success(conRuleService.selectConRuleByProjectId(projectId));
    }

    /**
     * 根据conId获取该合同下的装卸船计费规则的ruleId，合同下没有装卸船计费规则则返回-1
     * @param conId
     * @return
     */
    @PostMapping("/getDiscRuleByConId")
    public AjaxResult getDiscRuleByConId(@RequestBody Long conId){
        return AjaxResult.success(conRuleService.getDiscRuleByConId(conId));
    }


}
